package boggle;


import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.event.*;

public class JBoggleApp extends JFrame {
	
	private static WordList dict;
		
	private WordList wordsFound;
	private DefaultListModel<String> listModel; 
	
	private JButton start;      // start button
	private JButton addWord;    // add word button
	private JButton clearWord;  // clear word button
	private JButton newGame;    // end current game
	private JBoggleBoard board;  
	private BoggleBoard bgBoard;  
	private JScrollPane list;   // found word list
	private JPanel center;
	
	public JBoggleApp() {
		super("Boggle!");
		this.setLayout(new BorderLayout());

		try {
			String totalName = getClass().getResource("./sowpods.zip").getPath();
			dict =  WordList.loadFromZipFile(new File(totalName));
		}
		catch (IOException e) {
			System.out.println("Failed to create Dict!!");
			e.printStackTrace();
		}
				
		wordsFound = new WordList();
		listModel = new DefaultListModel<String> ();
		
		buildGUI();
       	
		addActionListenerForAll();
		setSize(new Dimension(500, 500));
		enableGUI(false);  // disable GUI at first.
		
	}


	private void addActionListenerForAll() {
		addWord.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String word = board.getWord().toLowerCase();
				//System.out.println("word:" + word);
				if (word.length() >= 3 && !wordsFound.containsWord(word) 
						&& dict.containsWord(word)) {
					wordsFound.add(word);
					listModel.addElement(word);
					
				}
				board.clearSelections();
			}
		});
		
		clearWord.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				board.clearSelections();
			}
		});
		
		newGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				endGame();
			}
		});

		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startGame();
			}
		});
	}


	private void buildGUI() {
		start = new JButton("Start Game");
		start.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		add(start, BorderLayout.NORTH);
		
		center = new JPanel();
		center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
		
		JBoggleTimer timer = new JBoggleTimer();
		center.add(timer);
				
		
		board = JBoggleBoard.generateGameStartBoard(BoggleBoard.DEFAULT_BOARD_SIZE);
		center.add(board);
		
		add(center, BorderLayout.CENTER);
		
		JPanel west = new JPanel();
		west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
		JList<String> tmplist = new JList<String>(listModel);
		tmplist.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
		list = new JScrollPane(tmplist);
		listModel.addElement("    ");
		JLabel wordsLabel = new JLabel("Your words:");
		wordsLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		west.add(wordsLabel);
		west.add(list);
				
		add(west, BorderLayout.WEST);
		
		JPanel south = new JPanel();
		south.setLayout(new FlowLayout());
		addWord    = new JButton("Add Word");
		clearWord  = new JButton("Clear Word");
		newGame    = new JButton("End Game");
		addWord.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		clearWord.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		newGame.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
				
		south.add(addWord);
		south.add(clearWord);
		south.add(newGame);
		
		add(south, BorderLayout.SOUTH);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		//setSize(650, 500);
        setVisible(true);
	}
	
		
	
	private void endGame() {
		board.clearSelections();
		WordList allWords = BoggleSolver.solve(bgBoard);
		
		listModel.addElement("Computer Words:");
		for (String word : allWords) {
			if (!wordsFound.containsWord(word))
				listModel.addElement(word);
		}
		
		enableGUI(false);
	}



	private void startGame() {
		// clear List
		wordsFound =  new WordList();
		listModel.clear();
		bgBoard = new BoggleBoard();
		board.setBoard(bgBoard);
		center.validate();
		enableGUI(true);
	}

	private void enableGUI(boolean b) {
		start.setEnabled(!b);
		newGame.setEnabled(b);
		addWord.setEnabled(b);
		clearWord.setEnabled(b);
		//board.setEnabled(b);
		if (!b)  board.disableAllButtons();
		
	}
	



	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {  
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");     
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		
		new JBoggleApp();

	}

}
